{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import librosa\n",
    "from tqdm import tqdm\n",
    "dataset_path = \"data/\"\n",
    "test_list = \"testing_list.txt\"\n",
    "valid_list = \"validation_list.txt\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'testing_list.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mtest_list\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[0;32m      2\u001b[0m     test_file \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28mlen\u001b[39m(test_file)):\n",
      "File \u001b[1;32md:\\conda\\miniconda\\envs\\whisper\\lib\\site-packages\\IPython\\core\\interactiveshell.py:310\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    304\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    305\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    306\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    307\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    308\u001b[0m     )\n\u001b[1;32m--> 310\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testing_list.txt'"
     ]
    }
   ],
   "source": [
    "with open(test_list, \"r\") as f:\n",
    "    test_file = f.readlines()\n",
    "for i in range(len(test_file)):\n",
    "    test_file[i] = test_file[i].strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bed': 0,\n",
       " 'wow': 1,\n",
       " 'five': 2,\n",
       " 'left': 3,\n",
       " 'down': 4,\n",
       " 'forward': 5,\n",
       " 'sheila': 6,\n",
       " 'go': 7,\n",
       " 'dog': 8,\n",
       " 'up': 9,\n",
       " 'zero': 10,\n",
       " 'yes': 11,\n",
       " 'learn': 12,\n",
       " 'two': 13,\n",
       " 'right': 14,\n",
       " 'four': 15,\n",
       " 'eight': 16,\n",
       " 'cat': 17,\n",
       " 'happy': 18,\n",
       " 'six': 19,\n",
       " 'tree': 20,\n",
       " 'bird': 21,\n",
       " 'follow': 22,\n",
       " 'marvin': 23,\n",
       " 'on': 24,\n",
       " 'stop': 25,\n",
       " 'one': 26,\n",
       " 'visual': 27,\n",
       " 'house': 28,\n",
       " 'off': 29,\n",
       " 'seven': 30,\n",
       " 'nine': 31,\n",
       " 'no': 32,\n",
       " 'three': 33,\n",
       " 'backward': 34}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# preoapre for the label list\n",
    "targets = os.listdir(dataset_path)\n",
    "for t in targets:\n",
    "    if t.startswith(\".\"):\n",
    "        targets.remove(t)\n",
    "target_map = {}\n",
    "for i, t in enumerate(targets):\n",
    "    target_map[t] = i\n",
    "target_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 36/36 [00:00<00:00, 290.20it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "105829"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prepare for the training files\n",
    "train_file = []\n",
    "for t in tqdm(os.listdir(dataset_path)):\n",
    "    for st in os.listdir(os.path.join(dataset_path, t)):\n",
    "        train_file.append(os.path.join(t, st))\n",
    "len(train_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "94824"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_file = list(set(train_file) - set(test_file))\n",
    "len(train_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 11005/11005 [00:00<00:00, 13387.43it/s]\n"
     ]
    }
   ],
   "source": [
    "output_dicts = []\n",
    "for f in tqdm(test_file):\n",
    "    y, sr = librosa.load(os.path.join(dataset_path, f), sr = None)\n",
    "    temp_dict = {\n",
    "        \"name\": f,\n",
    "        \"target\": target_map[f.split(\"/\")[0]],\n",
    "        \"waveform\": y\n",
    "    }\n",
    "    output_dicts.append(temp_dict)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(\"scv2_test.npy\", output_dicts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "whisper",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
